// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi 
// Source File Name:   TreeDic.java

package com.vandagroup.common.treedic;

import java.io.PrintStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

// Referenced classes of package com.vandagroup.common.treedic:
//            DBConnFactory, DBOpr, TreeNode

public class TreeDic
{

    private String dicID;
    private boolean dicType;
    private String dicName;
    private String dicDefine;
    private boolean isNeedReload;
    private int maxReloadTime;
    private int priotiry;
    private TreeNode treeNode;
    private HashMap keyValMapping;
    private HashMap nodeIndexMapping;
    private long lastLoadTimeStamp;
    private int maxLevel;

    public static TreeDic getInstance(String dicID)
        throws ClassNotFoundException, SQLException
    {
        TreeDic treeDic = new TreeDic(dicID);
        treeDic.load();
        return treeDic;
    }

    private TreeDic(String dicID)
    {
        maxLevel = 1;
        this.dicID = dicID;
        nodeIndexMapping = new HashMap();
    }

    protected void load()
        throws ClassNotFoundException, SQLException
    {
        initParas();
        initTree();
    }

    private void initParas()
        throws ClassNotFoundException, SQLException
    {
        String sql = "select SYSID, DICID, DICTYPE, DICNAME, DICDEFINE, ISNEEDRELOAD, MAXRELOADTIME, PRIOTIRY from CacheDicDef where DICID = '" + dicID + "'";
        System.out.println("initParas::" + sql);
        java.sql.Connection conn = DBConnFactory.getConnection();
        try
        {
            DBOpr dbOpr = new DBOpr();
            dbOpr.setConnection(conn);
            ArrayList parasRslt = dbOpr.query(sql);
            if(parasRslt.size() != 1)
                throw new IllegalStateException("Dictionary Define Error in table CacheDicTree");
            ArrayList v = (ArrayList)parasRslt.get(0);
            dicID = (String)v.get(1);
            String tmpDicType = (String)v.get(2);
            try
            {
                int iDicType = Integer.parseInt(tmpDicType);
                if(iDicType == 0)
                    dicType = false;
                else
                    dicType = true;
            }
            catch(Exception exception) { }
            dicName = (String)v.get(3);
            dicDefine = (String)v.get(4);
            String tmpNeedReload = (String)v.get(5);
            try
            {
                int needReload = Integer.parseInt(tmpNeedReload);
                if(needReload == 1)
                    isNeedReload = true;
                else
                    isNeedReload = false;
            }
            catch(Exception exception1) { }
            String tmpMaxReloadTime = (String)v.get(6);
            try
            {
                maxReloadTime = Integer.parseInt(tmpMaxReloadTime);
            }
            catch(Exception exception2) { }
            String tmpPriotiry = (String)v.get(7);
            try
            {
                priotiry = Integer.parseInt(tmpPriotiry);
            }
            catch(Exception e) { }
        }
        catch(SQLException e)
        {
            throw e;
        }
        finally
        {
            DBConnFactory.freeConnection(conn);
        }
        return;
    }

    protected void initTree()
        throws ClassNotFoundException, SQLException
    {
        java.sql.Connection conn = DBConnFactory.getConnection();
        try
        {
            DBOpr dbOpr = new DBOpr();
            dbOpr.setConnection(conn);
            ArrayList treeData = dbOpr.query(dicDefine);
            keyValMapping = dbOpr.getKeyValueMapping();
            int rsltCount = treeData.size();
            for(int i = 0; i < rsltCount; i++)
            {
                ArrayList oneNode = (ArrayList)treeData.get(i);
                String systemID = (String)oneNode.get(0);
                String parentID = (String)oneNode.get(1);
                String caption = (String)oneNode.get(2);
                TreeNode node = new TreeNode(systemID, parentID, caption);
                node.setTreeNodeAttribute(oneNode);
                nodeIndexMapping.put(systemID, node);
                if(i == 0)
                {
                    treeNode = node;
                } else
                {
                    TreeNode parentNode = (TreeNode)nodeIndexMapping.get(parentID);
                    if(parentNode == null)
                    {
                        System.out.println("TreeNode data info error, cannot find parent node:" + parentID);
                    } else
                    {
                        parentNode.addChildNode(node);
                        int currLevels = parentNode.getCurrentLevel();
                        if(currLevels > maxLevel)
                            maxLevel = currLevels;
                    }
                }
            }

            lastLoadTimeStamp = System.currentTimeMillis();
        }
        catch(SQLException e)
        {
            throw e;
        }
        finally
        {
            DBConnFactory.freeConnection(conn);
        }
        return;
    }

    protected void reload()
        throws ClassNotFoundException, SQLException
    {
        load();
    }

    public String[][] getArrayTree()
    {
        return getArrayTree(maxLevel);
    }

    public String[][] getArrayTree(String systemID)
    {
        return getArrayTree(systemID, maxLevel, true);
    }

    public String[][] getArrayTree(int level)
    {
        return getArrayTree(treeNode.getSystemID(), level, true);
    }

    public String[][] getArrayTree(String systemID, int level, boolean isIncludeParentNodeInfo)
    {
        ArrayList array = new ArrayList();
        TreeNode node = (TreeNode)nodeIndexMapping.get(systemID);
        if(isIncludeParentNodeInfo)
        {
            String value[] = {
                node.getSystemID(), node.getParentID(), node.getCaption()
            };
            array.add(value);
        }
        node.getArrayTree(node, array, 1, level);
        String result[][] = new String[array.size()][];
        array.toArray(result);
        return result;
    }

    public String getNodeCaption(String systemID)
    {
        TreeNode treeNode = (TreeNode)nodeIndexMapping.get(systemID);
        return treeNode.getCaption();
    }

    public HashMap getNodeAttribute(String systemID)
    {
        TreeNode treeNode = (TreeNode)nodeIndexMapping.get(systemID);
        ArrayList arrayAttrs = treeNode.getTreeNodeAttribute();
        HashMap mapAttrs = new HashMap();
        for(int i = 0; i < arrayAttrs.size(); i++)
        {
            String key = (String)keyValMapping.get(new Integer(i));
            String value = (String)arrayAttrs.get(i);
            mapAttrs.put(key, value);
        }

        return mapAttrs;
    }
}
